<%@ Control Language="C#" Inherits="SoftwareMonkeys.Scriptech.Web.BaseScript" %>
<%@ Import namespace="SoftwareMonkeys.Scriptech.Configuration" %>
<%@ Import namespace="SoftwareMonkeys.Scriptech.Projects" %>
<%@ Import namespace="SoftwareMonkeys.Scriptech.Web" %>
<%@ Import namespace="SoftwareMonkeys.Scriptech.Ftp" %>
<%@ Import namespace="SoftwareMonkeys.Scriptech.Diagnostics" %>
<%@ Import namespace="SoftwareMonkeys.Scriptech.Business" %>
<script runat="server">
protected Project CurrentProject = null;

private void Page_Load(object sender, EventArgs e)
{
	if (!IsPostBack)
	{
		string projectName = Request["Project"];

		CurrentProject = ProjectReader.GetProject(Request["Store"], Request["Group"], projectName);

		FtpServer[] servers = IndexStrategy.New<FtpServer>().Index<FtpServer>();
		ReportsFtpServer.DataSource = servers;
		StagingFtpServer.DataSource = servers;

		DataBind();
	}
}

private void UpdateButton_Click(object sender, EventArgs e)
{
	Project project = ProjectReader.GetProject(Request["Store"], Request["Group"], Request["Project"]);

	project.Name = ProjectName.Text;
	project.AssembliesDirectory = AssembliesDirectory.Text;
	project.BuildOrder = BuildOrder.Text.Trim().Split(Environment.NewLine.ToCharArray());
	project.ReportsServerID = (ReportsFtpServer.SelectedValue != null && ReportsFtpServer.SelectedValue != String.Empty)
			? new Guid(ReportsFtpServer.SelectedValue)
			: Guid.Empty;
	project.StagingServerID = (StagingFtpServer.SelectedValue != null && StagingFtpServer.SelectedValue != String.Empty)
			? new Guid(StagingFtpServer.SelectedValue)
			: Guid.Empty;

	ProjectSaver.SaveProject(Request["Store"],
		Request["Group"],
		project);

	Response.Redirect(WebUtilities.MapUrlPath("/Scripts/Project.script?Group=" + Request["Group"] + "&Store=" + Request["Store"] + "&Project=" + project.Name));

}

protected string GetBuildOrder()
{
	return  (CurrentProject != null && CurrentProject.BuildOrder != null && CurrentProject.BuildOrder.Length > 0)
		? String.Join(Environment.NewLine, CurrentProject.BuildOrder)
		: String.Empty;

	/*string buildOrderString = String.Empty;

	if (CurrentProject != null && CurrentProject.BuildOrder != null && CurrentProject.BuildOrder.Length > 0)
	{
		foreach (string item in CurrentProject.BuildOrder)
		{
			buildOrderString = buildOrderString + item + "\r\n";
		}
	}

	return buildOrderString;*/

}
</script>
	<h1>Edit Project</h1>
	<table width="100%">
		<tr>
			<td>Store name:</td>
			<td>
				<%= Request["Store"] %></td>
		</tr>
		<tr>
			<td>Group name:</td>
			<td>
				<%= Request["Group"] %></td>
		</tr>
		<tr>
			<td>Project name:</td>
			<td>
				<asp:textbox runat="server" id="ProjectName" width="400px" text='<%# CurrentProject.Name %>'/></td>
		</tr>
		<tr>
			<td>Assemblies folder:</td>
			<td>
				<asp:textbox runat="server" id="AssembliesDirectory" width="400px" text='<%# CurrentProject.AssembliesDirectory %>'/></td>
		</tr>
		<tr>
			<td>Build order (one per line):</td>
			<td>
				<asp:textbox runat="server" id="BuildOrder" TextMode="MultiLine" Rows="8" width="400px" text='<%# GetBuildOrder() %>'/></td>
		</tr>
		<tr>
			<td>Reports FTP server:</td>
			<td>
				<asp:DropDownList runat="Server" id="ReportsFtpServer" DataTextField="Name" DataValueField="ID" /></td>
		</tr>
		<tr>
			<td>Staging FTP server:</td>
			<td>
				<asp:DropDownList runat="Server" id="StagingFtpServer" DataTextField="Name" DataValueField="ID" /></td>
		</tr>
		<tr>
			<td> </td>
			<td>
				<asp:button runat="server" id="UpdateButton" text="Update" onclick="UpdateButton_Click"/>
			</td>
		</tr>
	</table>